#include <stdlib.h>
#include "bird.h"
#include "animation.h"
#include "point2.h"
#include "globals.h"
#include "frame.h"
#include "rect.h"

Bird::Bird(){

	ani_move =NULL;	
	ani_dead =NULL;
}

Bird::~Bird(){

	delete ani_move;
	ani_move =NULL;	
	delete ani_dead;
	ani_dead =NULL;
}
/*
void Bird::setState(int state, int argument){
	
	switch(state){
	case STATE_STILL:
		cur_animation =ani_still;
		break;
	case STATE_DEAD:
		cur_animation =ani_dead;
//		set_func =characterDead;
		break;
	case STATE_MOVE:
		cur_animation =ani_move;
//		set_func =characterMove;
		break;
	}

	cur_animation->begin_time =0;
	cur_animation->cur_frame =0;
	this->state =state;
}
*/

void Bird::setStill(int argument){

	cur_animation =ani_still;
	set_func =still;

	cur_animation->begin_time =0;
	cur_animation->cur_frame =0;
	this->state =STATE_STILL;
};

void Bird::setMove(int argument){

	cur_animation =ani_move;
	set_func =move;

	cur_animation->begin_time =0;
	cur_animation->cur_frame =0;
	this->state =STATE_MOVE;
};

void Bird::setJump(int argument){};

void Bird::setFall(int argument){};

void Bird::setDead(int argument){

	cur_animation =ani_dead;
	set_func =die;

	cur_animation->begin_time =0;
	cur_animation->cur_frame =0;
	this->state =STATE_DEAD;
};

void Bird::setAttack(int argument){};


void Bird::update(Level* level, Point2* acc, float dt){

	int passed_time;
	int passed_frames;
	div_t div_tmp;

	(this->*set_func)(level, acc, dt);
/*
	switch(state){
	case STATE_STILL:
		still(level, acc, dt);
		break;
	case STATE_MOVE:
		move(level, acc, dt);
		break;
	case STATE_DEAD:
		die(level, acc, dt);
		break;
	}
*/

//	this->set_func(level, acc, dt);

	// update animation
	Source* source;
	unsigned int fr1 =static_cast<unsigned int>(cur_animation->cur_frame);
	cur_animation->begin_time +=static_cast<unsigned int>(dt);
	passed_time =cur_animation->begin_time;

	passed_frames =(int)((float)passed_time/(1000.0f/cur_animation->fps));
	div_tmp =div(passed_frames, cur_animation->nFrames);
	cur_animation->cur_frame =div_tmp.rem;

	// if the frame is changed - //?
	if(cur_animation->cur_frame!=fr1){
		source =cur_animation->sources[cur_animation->cur_frame];
//		if(cur_animation->cur_frame==0)
//			srcPlay(this); 
		if(source)
			srcPlay(this); 
	}
}

void Bird::still(Level* level, Point2* acc, float dt){
	if(moveXplus){
//		orientation->x =1;
//		setState(STATE_MOVE, 0);
		setMove(0);
	}
	else if(moveXminus){
//		orientation->x =-1;
//		setState(STATE_MOVE, 0);
		setMove(0);
	}
}

void Bird::move(Level* level, Point2* acc, float dt){

	Point2 v;

	if(moveXplus){
		velocity->x =speed;
		orientation->x =1;
	}
	else if(moveXminus){
		velocity->x =-speed;	
		orientation->x =-1;
	}
	else{
		velocity->x =0;
//		setState(STATE_STILL, 0);
		setStill(0);
	}

	v.y =0;
	if(orientation->x >0)
		v.x =1;
	else 
		v.x =-1;
	v.Normalize();

	// find translation vector
	v =v*(speed*dt*0.1f);	// (m/s) 100cm/1000ms
	*position +=v;

//	srcPlay(this);
}

void Bird::die(Level* level, Point2* acc, float dt){
	// find velocity vector
	*velocity += *acc*(dt*0.001f);
	// find translation vector
	*position +=*velocity*(float)dt*0.1f; // 100cm/1000ms
}

float Bird::get_xLo(){

	return position->x + cur_animation->frames[cur_animation->cur_frame]->body->xLo;
}

float Bird::get_xHi(){

	return position->x + cur_animation->frames[cur_animation->cur_frame]->body->xHi;
}

float Bird::get_yHi(){

	return position->y + cur_animation->frames[cur_animation->cur_frame]->body->yHi;
}

float Bird::get_yLo(){

	return position->y + cur_animation->frames[cur_animation->cur_frame]->body->yLo;
}

float Bird::get_pic_xLo(){

	return position->x + cur_animation->frames[cur_animation->cur_frame]->box->xLo;
}

float Bird::get_pic_xHi(){

	return position->x + cur_animation->frames[cur_animation->cur_frame]->box->xHi;
}

float Bird::get_pic_yHi(){

	return position->y + cur_animation->frames[cur_animation->cur_frame]->box->yHi;
}

float Bird::get_pic_yLo(){

	return position->y + cur_animation->frames[cur_animation->cur_frame]->box->yLo;
}

Rect* Bird::getBody(){

	return cur_animation->frames[cur_animation->cur_frame]->body;
}

Rect* Bird::getBox(){

	return cur_animation->frames[cur_animation->cur_frame]->box;
}

